package com.day12.study.threadlocal_demo;

import javax.swing.*;

public class SaleTicket {
    public static void main(String[] args) {
        Station station = new Station();
        new Thread(station).start();
        new Thread(station).start();
        new Thread(station).start();
        new Thread(station).start();
    }
}

class Station implements Runnable {
    private Integer ticket = 5;
    private static final ThreadLocal<Integer> local = new ThreadLocal<>();

    @Override
    public void run() {
        while (true) {
            saleTicket();
        }
    }

    public void saleTicket() {
        Integer t = local.get();
        if (t == null) {
            local.set(this.ticket);
            t = local.get();
        }
        if (t > 0) {
            System.out.println(Thread.currentThread().getName() + ":" + t--);
        }
        local.set(t);
    }
}
